/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbTraceFilterRule;
import storage.util.StorageManager;
/**
 * 
 * @author fenyo rome
 *
 */
public class TraceFilterRuleHelper {
	/**
	 * 
	 * @param traceFilterRule
	 * traceFilterRule
	 * 
	 */
	public void saveTraceFilterRule(DbTraceFilterRule traceFilterRule) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_traceFilterRule"));
		// sm.open();
		sm.setRowKey(traceFilterRule.getRowKey());
		sm.put(DbTraceFilterRule.CL_NAME, traceFilterRule.getName());
		sm.put(DbTraceFilterRule.CL_TREACEFILTERRULE,
				traceFilterRule.getTraceFilterRule());
		sm.put(DbTraceFilterRule.CL_FILTERTYPE, traceFilterRule.getFilterType());
		sm.put(DbTraceFilterRule.CL_CREATETIME, traceFilterRule.getCreateTime());
		// sm.close();
	}

	/**
	 * 
	 * @param columnName
	 * columnName
	 * @param value
	 * value
	 * @return
	 */
	public List<DbTraceFilterRule> getTraceFilterRules(final String columnName,
			final String value) {
		List<DbTraceFilterRule> result = new LinkedList<DbTraceFilterRule>();
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_traceFilterRule"));
		// sm.open();
		List<KeySlice> crs = sm.get(columnName, value);
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String name = null, traceFilterRule = null, createTime = null, filterType = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbTraceFilterRule.CL_NAME)) {
						name = columnsValue;
					} else if (columnsName
							.equals(DbTraceFilterRule.CL_TREACEFILTERRULE)) {
						traceFilterRule = columnsValue;
					} else if (columnsName
							.equals(DbTraceFilterRule.CL_CREATETIME)) {
						createTime = columnsValue;
					} else if (columnsName
							.equals(DbTraceFilterRule.CL_FILTERTYPE)) {
						filterType = columnsValue;
					}
					DbTraceFilterRule pLogTraceFilterRule = new DbTraceFilterRule(
							rowKey, name, traceFilterRule, filterType,
							createTime);
					result.add(pLogTraceFilterRule);
				}
			}
		} catch (Exception ex) {
			Logger.getLogger(TraceFilterRuleHelper.class.getName()).log(
					Level.SEVERE, null, ex);
		} 
		return result;
	}
	/**
	 * 
	 * @return ArrayList<String>
	 */
	public ArrayList<String> getAllTraceFilterName() {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_traceFilterRule"));
		List<KeySlice> crs = sm.getAll();
		ArrayList<String> traceFilterRuleNameList = new ArrayList<String>();
		try {
			for (KeySlice ks : crs) {
				String name = "";
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbTraceFilterRule.CL_NAME)) {
						name = columnsValue;
					}
				}
				if (!traceFilterRuleNameList.contains(name)
						&& name.length() > 0) {
					traceFilterRuleNameList.add(name);
				}
			}
		} catch (Exception ex) {
			Logger.getLogger(TraceFilterRuleHelper.class.getName()).log(
					Level.SEVERE, null, ex);
		} 
		return traceFilterRuleNameList;
	}
	/**
	 * 
	 * @param rowKey
	 * rowKey
	 */
	public void deleteTraceFilterRule(final String rowKey) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_traceFilterRule"));
		sm.delete(rowKey);
		// sm.close();
	}
}
